package cn.enilu.flash.service.task.job;

import cn.enilu.flash.bean.entity.order.TaskOrder;
import cn.enilu.flash.bean.entity.task.HelpTask;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.order.TaskOrderService;
import cn.enilu.flash.service.task.HelpTaskService;
import cn.enilu.flash.service.task.JobExecuter;
import cn.enilu.flash.service.task.MoneyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 订单相关任务
* @author: 韦武朝
* @date: 2022/4/8 21:49
* @Return:
*/
@Component
public class TaskOrderJob extends JobExecuter {
    @Autowired
    private HelpTaskService helpTaskService;
    @Autowired
    private TaskOrderService taskOrderService;
    @Autowired
    private MoneyInfoService moneyInfoService;
    @Override
    public void execute(Map<String, Object> dataMap) throws Exception {
        List<SearchFilter> searchFilters = new ArrayList<>();
        searchFilters.add(SearchFilter.build("autoWaiverTime",SearchFilter.Operator.ISNOTNULL));
        searchFilters.add(new SearchFilter("autoWaiverTime", SearchFilter.Operator.LTE,new Date()));
        searchFilters.add(new SearchFilter("state", SearchFilter.Operator.EQ,4));
        List<TaskOrder> taskOrders = taskOrderService.queryAll(searchFilters);
        cancelOrder(taskOrders);
        searchFilters = new ArrayList<>();
        searchFilters.add(SearchFilter.build("timeLimit",SearchFilter.Operator.ISNOTNULL));
        searchFilters.add(new SearchFilter("timeLimit", SearchFilter.Operator.LTE,new Date()));
        searchFilters.add(new SearchFilter("state", SearchFilter.Operator.EQ,1));
        taskOrders = taskOrderService.queryAll(searchFilters);
        cancelOrder(taskOrders);
        searchFilters = new ArrayList<>();
        searchFilters.add(SearchFilter.build("autoAuditTime",SearchFilter.Operator.ISNOTNULL));
        searchFilters.add(new SearchFilter("autoAuditTime", SearchFilter.Operator.LTE,new Date()));
        searchFilters.add(new SearchFilter("state", SearchFilter.Operator.EQ,2));
        taskOrders = taskOrderService.queryAll(searchFilters);
        for (TaskOrder taskOrder:taskOrders){
            taskOrder.setState(3);// 已完成
            taskOrder.setResult(1);
            taskOrder.setAuditTime(new Date());// 审核时间
            moneyInfoService.setMoneyInfo(taskOrder.getTaskId(),taskOrder.getCreateBy(),1,taskOrder.getMoney());//设置金额明细并变动用户金额
            taskOrderService.update(taskOrder);
        }
    }
    private void cancelOrder(List<TaskOrder> taskOrders){
        for (TaskOrder taskOrder:taskOrders){
            taskOrder.setState(6);// 取消订单
            TaskOrder update = taskOrderService.update(taskOrder);
            HelpTask helpTask = helpTaskService.get(update.getTaskId());
            helpTask.setLastNum(helpTask.getLastNum()+1);
            if(helpTask.getState() == 6 && helpTask.getLastNum() == 1){
                helpTask.setState(1);
            }
            helpTaskService.update(helpTask);
        }
    }
}
